package com.sk.coin.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.coin.sys.dto.RolePrivilegesParam;
import com.sk.coin.sys.entity.SysMenu;
import com.sk.coin.sys.entity.SysRolePrivilege;
import com.sk.coin.sys.mapper.SysRolePrivilegeMapper;
import com.sk.coin.sys.service.ISysMenuService;
import com.sk.coin.sys.service.ISysPrivilegeService;
import com.sk.coin.sys.service.ISysRolePrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限配置 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2022-02-21
 */
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements ISysRolePrivilegeService {

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysPrivilegeService sysPrivilegeService;

    @Override
    public List<SysMenu> findSysMenuAndPrivileges(Long roleId) {
        // 查询所有的菜单
        List<SysMenu> list = sysMenuService.list();
        // 页面上显示的是二级菜单，以及二级菜单包含的权限
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<SysMenu> rootMenus = list.stream()
                .filter(item -> item.getParentId() == null)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(rootMenus)) {
            return Collections.emptyList();
        }
        // 查询所有的二级菜单
        List<SysMenu> subMenus = new ArrayList<>();
        for (SysMenu rootMenu : rootMenus) {
            subMenus.addAll(getChildMenus(rootMenu.getId(),roleId,list));
        }
        return subMenus;
    }

    /**
     * 给角色授权
     * @param rolePrivilegesParam 角色权限信息
     * @return 成功
     */
    @Override
    @Transactional
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        Long roleId = rolePrivilegesParam.getRoleId();
        // 删除之前的值
        remove(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId,roleId));
        List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
        List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
        if (!CollectionUtils.isEmpty(privilegeIds)) {
            for (Long privilegeId : privilegeIds) {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setRoleId(roleId);
                sysRolePrivilege.setPrivilegeId(privilegeId);
                sysRolePrivileges.add(sysRolePrivilege);
            }
        }
        return saveBatch(sysRolePrivileges);
    }


    /**
     * 递归查询菜单的子菜单
     * @param parentId 父菜单的Id
     * @param roleId   当前查询的角色Id
     * @param sources  查询的资源
     * @return 所有的子菜单
     */
    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources){
        List<SysMenu> children = new ArrayList<>();
        for (SysMenu source : sources) {
            if (source.getParentId()!= null && source.getParentId().equals(parentId)) {
                children.add(source);
                source.setChilds(getChildMenus(source.getId(),roleId,sources));
                source.setPrivileges(sysPrivilegeService.getAllSysPrivilege(source.getId(),roleId));
            }
        }
        return children;
    }
}
